import type { App } from 'vue'
import { createI18n } from 'vue-i18n'
import { useLocaleStore } from '@/stores/modules/locale'
import { changeLocale } from './util'

const i18n = createI18n({
  legacy: false,
  globalInjection: true,
  // locale: locale, // 默认语言
  fallbackLocale: 'en', // 回退语言
  silentTranslationWarn: true,
  missingWarn: false,
  silentFallbackWarn: true,
  messages: {},
})

const $t = i18n.global.t

async function setupI18n(app: App) {
  const { locale } = useLocaleStore()
  await changeLocale(locale)
  app.use(i18n)
}

export {
  $t,
  i18n,
  setupI18n,
}
